import Login from "@/views/Login/login.vue"
import {
  createRouter,
  createWebHashHistory,
  useRouter,
} from 'vue-router'
import Dept from "../views/dept.vue"
import Home from "../views/home.vue"
import Main from "../views/main.vue"
import Role from "../views/role.vue"
import User from "../views/user.vue"

import { usePermissionStore } from "@/store/permisson"
import Doctor from "@/views/Doctor/doctor.vue"
import NotFound from "../views/404.vue"
import DoctorPrice from "../views/doctor_price.vue"
import DoctorSchedule from "../views/doctor_schedule.vue"
import MedicalDept from "../views/medical_dept.vue"
import MedicalDeptSub from "../views/medical_dept_sub.vue"
import MedicalDeptSubWorkPlan from "../views/medical_dept_sub_work_plan.vue"
import VideoDiagnose from "../views/video_diagnose.vue"



const routes = [{
  path: '/login',
  name: 'Login',
  component: Login
},
{
  path: '/',
  name: 'Main',
  component: Main,
  children: [{
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      title: '首页',
    }
  },
  {
    path: "/role",
    name: "Role",
    component: Role,
    meta: {
      title: "角色管理",
      isTab: true
    }
  },
  {
    path: '/user',
    name: 'User',
    component: User,
    meta: {
      title: '用户管理',
      isTab: true
    }
  },
  {
    path: '/dept',
    name: 'Dept',
    component: Dept,
    meta: {
      title: '部门管理',
      isTab: true
    }
  },
  {
    path: '/medical_dept',
    name: 'MedicalDept',
    component: MedicalDept,
    meta: {
      title: '医疗科室管理',
      isTab: true
    }
  },
  {
    path: '/medical_dept_sub',
    name: 'MedicalDeptSub',
    component: MedicalDeptSub,
    meta: {
      title: '医疗诊室管理',
      isTab: true
    }
  },
  {
    path: '/doctor',
    name: 'Doctor',
    component: Doctor,
    meta: {
      title: '医生管理',
      isTab: true
    }
  },
  {
    path: '/doctor_price',
    name: 'DoctorPrice',
    component: DoctorPrice,
    meta: {
      title: '诊费设置',
      isTab: true
    }
  },
  {
    path: '/medical_dept_sub_work_plan',
    name: 'MedicalDeptSubWorkPlan',
    component: MedicalDeptSubWorkPlan,
    meta: {
      title: '门诊日程表',
      isTab: true
    }
  },

  {
    path: '/doctor_schedule/:deptName/:deptSubId/:date',
    name: 'DoctorSchedule',
    component: DoctorSchedule,
    meta: {
      title: '医生出诊表',
      isTab: true
    }
  },
  {
    path: '/video_diagnose',
    name: 'VideoDiagnose',
    component: VideoDiagnose,
    meta: {
      title: '视频问诊',
      isTab: true
    }
  },


  ]
},
{
  path: "/404",
  name: "NotFound",
  component: NotFound
},
{
  path: '/:pathMatch(.*)*',
  redirect: "/404"
}
]
const whiteList = [ 'Login', 'NotFound' ]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach( to => {
  const { authData: { permissions, token } } = usePermissionStore()
  const router = useRouter()
  const isAuth = permissions.filter(el => el).length || token
  
  if(!whiteList.includes(to.name as string) && !isAuth) {
    return {
      name:'Login',
      query: {
        redirect: router.currentRoute.value.fullPath
      }
    }
  
  }
  if(isAuth && to.name == 'Login'){
    return '/home'
  }
})

export default router
